#ifndef SLANPROJECTFILEDOCK_CPP
#define SLANPROJECTFILEDOCK_CPP

#include "slanprojectfiledock.h"
#include "slanEdit.h"
#include <QTreeView>
#include <QTreeWidget>
#include <QDir>
#include <QInputDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QHeaderView>
#include <QHBoxLayout>


SlanProjectFileDock::SlanProjectFileDock(const QString &name, QWidget *parent)
    : QDockWidget(name,parent)
{
    this->parent = parent;

    QTreeView *tree = new QTreeView(this);
    this->setWidget(tree);
    createWidgets();
    createContextMenu();



}

void SlanProjectFileDock::createWidgets()
{
    std::for_each(actionArray, actionArray + MenuNumMax, [this](QAction*& item){
        item = new QAction(this);
        item->setVisible(true);
    });

    //创建树形控件
    projectTree = new QTreeWidget(this);
    projectTree->setColumnCount(1);//列数
    projectTree->header()->setHidden(true);//隐藏表头
    setWidget(projectTree);
}

void SlanProjectFileDock::createContextMenu()
{

    actionArray[CreateFile]->setText(tr("新建文件"));
    connect(actionArray[CreateFile], &QAction::triggered,
            this, &SlanProjectFileDock::slotCreateFile);

    actionArray[CreateFolder]->setText(tr("新建目录"));
    connect(actionArray[CreateFolder], &QAction::triggered,
            this, &SlanProjectFileDock::createFolder);

    std::for_each(actionArray, actionArray + MenuNumMax, [this](QAction* item){
        this->addAction(item);
    });

    setContextMenuPolicy(Qt::ContextMenuPolicy::ActionsContextMenu);//
}

void SlanProjectFileDock::createProjectTree(QString &projectName, QString &path)
{
    homePath = path;
    QTreeWidgetItem* item = new QTreeWidgetItem(projectTree);

    item->setData(0, Qt::UserRole, 1);
//    item->setData(0, Qt::UserRole+1, projectName);
    item->setText(0, projectName);
    item->setIcon(0, QIcon(":/images/folder.png"));//设置图标
}

void SlanProjectFileDock::createFolder()
{
    QList<QTreeWidgetItem*> items = projectTree->selectedItems();

    QString folderName;
    QString path;
    QDir dir;
    qDebug() << homePath;
    folderName = QInputDialog::getText(this, "设置目录名称", "输入名称:");
    if ( folderName.isEmpty() )
        return ;

    path = homePath + QDir::separator() + folderName;

    //修改硬盘中的数据
    if (!dir.mkdir(path))
    {
        QMessageBox::warning(this, tr("错误"),
            tr("创建目录 %1 失败").arg(folderName));
        return;
    }

    QTreeWidgetItem* item = items.empty() ?
        new QTreeWidgetItem(projectTree) :
        new QTreeWidgetItem(items.front());

//    item->setData(0, ITEM_TYPE_KEY, Folder);
//    item->setData(0, FOLDER_NAME_KEY,path);
    item->setText(0, folderName);

    item->setIcon(0, QIcon(":/images/folder.png"));

}

void SlanProjectFileDock::createTreeWidgetItem(const QString &fileName, QTreeWidgetItem *parent)
{
    QTreeWidgetItem * child = NULL;

    child = parent == NULL ?
                new QTreeWidgetItem(projectTree) :
                new QTreeWidgetItem(parent);

    QFileInfo info(fileName);
    child->setText(0, info.completeBaseName());

    QString suffix = info.completeSuffix();
    if ( suffix == "c" )
        child->setIcon(0, QIcon(":/images/c.png"));
    else if ( suffix == "cpp" )
        child->setIcon(0, QIcon(":/images/cpp.png"));
    else if ( suffix == "h" )
        child->setIcon(0, QIcon(":/images/h.png"));
    //继续往下添加支持各种后缀图片


}

void SlanProjectFileDock::slotCreateFile()
{

    QList<QTreeWidgetItem*>items = projectTree->selectedItems();

    if ( items.isEmpty() )
        return ;

    QTreeWidgetItem* parent = items.front();

    QString fileName = QFileDialog::getSaveFileName(this, tr("新建文件"), ".", tr("*"));

    if ( !QFile(fileName).open(QIODevice::WriteOnly) )
    {
        QMessageBox::warning(this, "错误", tr("创建文件%1失败").arg(fileName));
        return ;
    }

    createTreeWidgetItem(fileName, parent);

    emit createdFile(fileName);
}



#endif // SLANPROJECTFILEDOCK_CPP
